package com.hys;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;

import org.bouncycastle.openssl.PEMWriter;
import org.bouncycastle.openssl.jcajce.JcaPEMWriter;

public class DirTOPem {

    public static void main(String[] args) throws Exception {
        // Convert Nginx certificate from DER to PEM format
        convertDerToPem("C:\\Users\\Administrator\\Desktop\\192.168.1.106\\certificate.crt",
                "C:\\Users\\Administrator\\Desktop\\192.168.1.106\\certificate.pem");

        // Generate PEM format certificate using Java
        generatePemCertificate();
    }

    private static void convertDerToPem(String derFilePath, String pemFilePath) throws Exception {
        // Load DER format certificate
        byte[] derBytes = java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(derFilePath));

        // Convert DER to PEM format
        org.bouncycastle.util.io.pem.PemObject pemObject = new org.bouncycastle.util.io.pem.PemObject("CERTIFICATE", derBytes);
        try (FileOutputStream pemOut = new FileOutputStream(pemFilePath);
             OutputStreamWriter pemWriter = new OutputStreamWriter(pemOut)) {
            JcaPEMWriter writer = new JcaPEMWriter(pemWriter);
            writer.writeObject(pemObject);
            writer.flush();
        }

        System.out.println("DER to PEM conversion completed successfully.");
    }

    private static void generatePemCertificate() throws Exception {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

        // Generate key pair
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        PrivateKey privateKey = keyPair.getPrivate();

        // Generate certificate
        X509Certificate cert = generateCertificate(keyPair);

        // Save private key and certificate to files
        try (FileOutputStream privateKeyOut = new FileOutputStream("private.key");
             FileOutputStream certificateOut = new FileOutputStream("certificate.pem");
             OutputStreamWriter pemWriter = new OutputStreamWriter(certificateOut)) {
            privateKeyOut.write(privateKey.getEncoded());

            JcaPEMWriter writer = new JcaPEMWriter(pemWriter);
            writer.writeObject(cert);
            writer.flush();
        }

        System.out.println("Private key and certificate generated successfully.");
    }

    private static X509Certificate generateCertificate(KeyPair keyPair) throws Exception {
        // Generate and return the X509Certificate object
        // This is just a placeholder method, you need to implement the certificate generation logic here
        // You can use the Bouncy Castle library to generate the certificate
        // Refer to the Bouncy Castle documentation for more information on generating certificates
        throw new UnsupportedOperationException("Certificate generation logic not implemented");
    }

}
